import cookie from 'js-cookie';
import storage from './storage.js';

const ACCESS_TOKEN = 'access_token';

export function clearToken() {
  cookie.remove(ACCESS_TOKEN);
  storage.session.removeItem(ACCESS_TOKEN);
}

export function setToken(token, expires) {
  const option = {
    expires: expires !== undefined ? expires : 7,
  };
  if (location.protocol === 'https:') {
    option.sameSite = 'none';
    option.secure = true;
  }
  cookie.set(ACCESS_TOKEN, token, option);
  storage.session.setItem(ACCESS_TOKEN, token, option);
}

export function getToken() {
  return storage.session.getItem(ACCESS_TOKEN) || cookie.get(ACCESS_TOKEN);
}

export function hasToken() {
  return !!getToken();
}
